package com.edu.control.deploy.sync;

import com.alibaba.fastjson.JSON;
import com.edu.control.deploy.DeployResult;
import com.edu.control.deploy.DeployStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * flow操作相关的redis类
 *
 * @Author 86000012
 * @Date 2020-09-16
 */
@Component("deployStorageRedis")
public class DeployStorageRedis implements DeployStorage {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeployStorageRedis.class);

    @Resource
    private ApplicationContext context;

    private final static String prefix = "OPENFAAS:DEPLOY:";

    private static StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void init() {
        stringRedisTemplate = context.getBean(StringRedisTemplate.class);
    }

    public static void set(String key, Object object) {
        String v = JSON.toJSONString(object);
        LOGGER.info("key {} , value {}", prefix + key, v);
        stringRedisTemplate.opsForValue().set(prefix + key, v);
    }

    public static String get(String key) {
        return stringRedisTemplate.opsForValue().get(prefix + key);
    }

    public static void del(String key) {
        stringRedisTemplate.delete(prefix + key);
    }

    @Override
    public void store(String key, DeployResult deployResult) {
        set(key, deployResult);
    }
}
